package com.ruoyi.provider.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.provider.domain.AhctDefineDamageProject;
import com.ruoyi.provider.domain.AhctImage;
import com.ruoyi.provider.mapper.AhctImageMapper;
import com.ruoyi.provider.service.AhctDefineDamageProjectService;
import com.ruoyi.provider.service.AhctImageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
* @author Wanmg RuLong
* @description 针对表【ahct_image(图片素材管理表)】的数据库操作Service实现
* @createDate 2024-05-18 15:06:50
*/
@Service
public class AhctImageServiceImpl extends ServiceImpl<AhctImageMapper, AhctImage>
    implements AhctImageService{

    @Resource
    private AhctImageMapper ahctImageMapper;

    @Resource
    private AhctDefineDamageProjectService ahctDefineDamageProjectService;


    //新增查勘图片
    @Override
    public void insertImage(Integer id, String case_number, List<String> imgList) {

        ArrayList<AhctImage> list = new ArrayList<>();
        if (imgList.size() >= 1) {
            int i = 0;//定义一个i为0
                for (String img : imgList) {
                    AhctImage ahctImage = new AhctImage();
                    ahctImage.setId(id);
                    ahctImage.setCase_number(case_number);
                    ahctImage.setImage_url(img);
                    ahctImage.setType(1);
                    ahctImage.setProject_id(id);
                    ahctImage.setSort(i++);//i++自增1 用于照片排序
                    ahctImage.setCreate_by("admin");
                    ahctImage.setUpdate_by("admin");
                    list.add(ahctImage);
                }
            ahctImageMapper.insertIma(list);
        }
    }


    //新增定损详情照片
    @Transactional
    @Override
    public void insertIma(AhctDefineDamageProject ahctDefineDamageProject) {

        ArrayList<AhctImage> list;

         int i = 0;//定义一个i为0}
        for (AhctDefineDamageProject img : ahctDefineDamageProject.getItemQuantity()) {
            list = new ArrayList<>();
            if (img.getImgList().size() >= 1) {
                List<String> image = new ArrayList<>(new HashSet<>(img.getImgList()));
                //查询出存项目列表时,自动生成id,将id遍历赋值给照片定损id
                Integer id = ahctDefineDamageProjectService.selectProjectId(img.getDamage_project_name(), img.getDefine_damage_id());
                for (String imga : image) {
                    AhctImage ahctImage = new AhctImage();
                    ahctImage.setCase_number(ahctDefineDamageProject.getCase_number());
                    ahctImage.setImage_url(imga);
                    ahctImage.setType(1);
                    ahctImage.setProject_id(id);
                    ahctImage.setSort(i++);//i++自增1 用于照片排序
                    ahctImage.setCreate_by("admin");
                    ahctImage.setUpdate_by("admin");
                    list.add(ahctImage);
                }
            }
            if (list.size() >= 1){
                ahctImageMapper.insertIma(list);
            }
        }
    }


    //删除图片
    @Override
    public void deleteImg(String case_number) {
        ahctImageMapper.deleteImg(case_number);
    }


    //查看查勘图片
    @Override
    public List<AhctImage> getSurveyImage(String case_number) {
        List<AhctImage> imgList;
        imgList = ahctImageMapper.getImage(case_number);
        return imgList;
    }


    //删除图片
    @Override
    public void deleteImgOne(AhctImage ahctImage) {
        ahctImageMapper.deleteImgOne(ahctImage);
    }


    //获取定损列表详情照片
    @Override
    public List<AhctImage> getImage(String case_number) {

        List<AhctImage> imageList;
        imageList = ahctImageMapper.getImage(case_number);

        return imageList;
    }
}




